package com.woniuxy.common;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.woniuxy.dto.CarInsuranceDto;
import com.woniuxy.service.CarInsuranceService;

import java.util.ArrayList;
import java.util.List;

public class CarInsuranceExcelListener extends AnalysisEventListener<CarInsuranceDto> {
    //通过构造方法传入业务对象
    private CarInsuranceService carInsuranceService;

    public CarInsuranceExcelListener(CarInsuranceService carInsuranceService) {
        this.carInsuranceService = carInsuranceService;
    }

    //创建一个集合保存所有导入的数据
    private List<CarInsuranceDto> carInsuranceList= new ArrayList<>();

    //每条数据如何处理
    @Override
    public void invoke(CarInsuranceDto carInsuranceDto, AnalysisContext analysisContext) {
        //每条数据添加到集合中
        carInsuranceList.add(carInsuranceDto);
    }
    //数据导入完成后的操作
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //写入到数据库中
        rowCount = carInsuranceService.insertBatch(carInsuranceList);
    }
    //影响行数
    private int rowCount;
    //获取导入的行数
    public int getRowCount() {
        return this.rowCount;
    }
}
